<template>
  <!-- <el-dropdown trigger="click"> -->
  <div class="flex text-14px items-center">
    <span>欢迎！</span>
    <div class="avatar">
      <img :src="avatar" alt="avatar" />
    </div>
    <span class="mr-24px ml-4px">{{ loginName }}</span>
    <a @click="logout" class="text-[#1890ff] cursor-pointer"> 登出 </a>
  </div>
  <!-- infoDialog -->
  <InfoDialog ref="infoRef"></InfoDialog>
  <!-- passwordDialog -->
  <PasswordDialog ref="passwordRef"></PasswordDialog>
</template>

<script setup>
import { computed } from 'vue'
import { LOGIN_URL } from '@/config'
import { useRouter } from 'vue-router'
import { user_logout } from '@/api/modules/login'
import { useUserStore } from '@/stores/modules/user'
import { ElMessageBox, ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const avatar = computed(() => userStore.userInfo.avatar)

const loginName = computed(() => userStore.userInfo.loginName)

// 退出登录
const logout = () => {
  ElMessageBox.confirm('您是否确认退出登录?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // 1.执行退出登录接口
    await user_logout()

    // 2.清除 Token
    userStore.setToken('')
    userStore.setUserInfo({})

    // 3.重定向到登陆页
    router.replace(LOGIN_URL)
    ElMessage.success('退出登录成功！')
  })
}
</script>

<style scoped lang="scss">
.avatar {
  width: 25px;
  height: 25px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>
